/**
 * 初始化套餐管理详情对话框
 */
var VpnPackageInfoInfoDlg = {
    vpnPackageInfoInfoData : {}
};

/**
 * 清除数据
 */
VpnPackageInfoInfoDlg.clearData = function() {
    this.vpnPackageInfoInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
VpnPackageInfoInfoDlg.set = function(key, val) {
    this.vpnPackageInfoInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
VpnPackageInfoInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
VpnPackageInfoInfoDlg.close = function() {
    layer.closeAll();
    parent.layer.close(window.parent.VpnPackageInfo.layerIndex);
}

/**
 * 收集数据
 */
VpnPackageInfoInfoDlg.collectData = function() {
    this
    .set('id')
    .set('packageType')
    .set('packageName')
    .set('deviceCount')
    .set('price')
    .set('status')
    .set('priceUnit')
    .set('duration')
    .set('englishName')
    .set('taiwanName')
    .set("hkPrice")
    .set("actualPriceUnit")
    .set("appleProductId")
    .set("showSort");

}

/**
 * 提交添加
 */
VpnPackageInfoInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    if ($("#showSort").val() == null){
        Feng.error("套餐展示顺序不能为空")
    }else{

        //提交信息
        var ajax = new $ax(Feng.ctxPath + "/vpnPackageInfo/add", function(data){
            Feng.success("添加成功!");
            window.parent.VpnPackageInfo.refresh();
            VpnPackageInfoInfoDlg.close();
        },function(data){
            Feng.error(data.responseJSON.message + "!");
        });
        ajax.set(this.vpnPackageInfoInfoData);
        ajax.start();
    }

}

/**
 * 提交修改
 */
VpnPackageInfoInfoDlg.editSubmit = function() {
    if ($("#etShowSort").val() == null){
        Feng.error("套餐展示顺序不能为空")
    }else{

        var packageData = {};
        packageData["id"] = $("#id").val();
        packageData["packageType"] = $("#packageType").val();
        packageData["packageName"] = $("#packageName").val();
        packageData["deviceCount"] = $("#deviceCount").val();
        packageData["price"] = $("#price").val();
        packageData["status"] = $("#status").val();
        packageData["duration"] = $("#duration").val();
        packageData["englishName"] = $("#englishName").val();
        packageData["taiwanName"] = $("#taiwanName").val();
        packageData["hkPrice"] = $("#hkPrice").val();
        packageData["appleProductId"] = $("#appleProductId").val();
        packageData["showSort"] = $("#etShowSort").val();

        packageData["discount"] = $("#discount").val();
        packageData["hintZh"] = $("#hintZh").val();
        packageData["hintEn"] = $("#hintEn").val();
        packageData["hintTw"] = $("#hintTw").val();
        packageData["startTime"] = $("#startTime").val();
        packageData["endTime"] = $("#endTime").val();
        packageData["discountId"] = $("#discountId").val();
        packageData["discountStatus"] = $("#discountStatus").val();

        //提交信息
        var ajax = new $ax(Feng.ctxPath + "/vpnPackageInfo/updatePackage", function(data){
            Feng.success("套餐信息修改成功!");
            VpnPackageInfoInfoDlg.close();
            window.parent.VpnPackageInfo.refresh();
            layer.closeAll();
        },function(data){
            Feng.error(data.responseJSON.message);
        });
        ajax.set(packageData);
        ajax.start();
    }

}


VpnPackageInfoInfoDlg.addDiscount = function() {
    if ($("#packageList").val() == null){
        Feng.error("套餐不能为空")
    }else if($("#discount").val() == null || $("#discount").val() == ''){
        Feng.error("折扣不能为空")
    }else if($("#hintZh").val() == null || $("#hintZh").val() == ''
                || $("#hintEn").val() == null || $("#hintEn").val() == ''
                || $("#hintTw").val() == null || $("#hintTw").val() == ''){
        Feng.error("折扣提醒不能为空")
    }else if($("#startTime").val() == null || $("#startTime").val() == ''){
        Feng.error("开始时间不能为空")
    }else if($("#endTime").val() == null || $("#endTime").val() == ''){
        Feng.error("结束时间不能为空")
    }else{
        var addData = {};
        addData["packageId"] = $("#packageList").val();
        addData["discount"] = $("#discount").val();
        addData["hintZh"] = $("#hintZh").val();
        addData["hintEn"] = $("#hintEn").val();
        addData["hintTw"] = $("#hintTw").val();
        addData["startTime"] = $("#startTime").val();
        addData["endTime"] = $("#endTime").val();

        //提交信息
        var ajax = new $ax(Feng.ctxPath + "/vpnPackageInfo/addDiscount", function(data){
            Feng.success("套餐折扣添加成功!");
            window.parent.VpnPackageInfo.refresh();
            VpnPackageInfoInfoDlg.close();
        },function(data){
            Feng.error("套餐折扣添加失败!" + data.responseJSON.message + "!");
        });
        ajax.set(addData);
        ajax.start();
    }
};


VpnPackageInfoInfoDlg.selectPackageList = function(){
    var ajax = new $ax(Feng.ctxPath + "/vpnPackageInfo/shortPackageList", function (data) {

        var items = data;
        items.forEach(function (item) {
            $("#packageList").append("'<option value='"+item.packageId+"'>"+item.packageName+"</option>'");
        }) ;

    }, function (data) {
        Feng.error("套餐列表获取失败" + data.responseJSON.message + "!");
    });
    ajax.start();
};

VpnPackageInfoInfoDlg.showSortList = function(){
    for (let i = 1; i < 99; i++) {
        $("#showSort").append("'<option value='"+i+"'>"+i+"</option>'");
    }
};



VpnPackageInfoInfoDlg.etShowSortList = function(){
    for (let i = 1; i < 99; i++) {
        $("#etShowSort").append("'<option value='"+i+"'>"+i+"</option>'");
    }
    if ($("#etShowSortValue").val() != null && $("#etShowSortValue").val() != ''){
        $("#etShowSort").val($("#etShowSortValue").val());
    }
};


$(function() {
    VpnPackageInfoInfoDlg.showSortList();
    VpnPackageInfoInfoDlg.selectPackageList();
    VpnPackageInfoInfoDlg.etShowSortList();
});
